/*--------------------------------------------------------------------------------------*\
**
** scenario_info_window.cpp
**
** Heroes IV
** Copyright 2001, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "scenario_info_window.h"

#include "adventure_map.h"
#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bitmap_group_ptr.h"
#include "bitmap_group_window.h"
#include "bitmap_layer_cache_window.h"
#include "bound_handler.h"
#include "button.h"
#include "button_cache.h"
#include "external_string.h"
#include "format_string.h"
#include "game_replace_keywords.h"
#include "interface_help.h"
#include "player.h"
#include "standard_fonts.h"

extern t_button_cache g_large_button;
extern t_bitmap_group_cache g_banners_38;

t_bitmap_group_cache  g_difficulty_60_icons( "icons.difficulty_60" );
t_bitmap_group_cache  g_banners_55( "icons.flags.55" );

extern t_external_string const		k_text_map_description_title( "scene_info_description_title.misc" );

extern t_external_string const		k_default_loss_condition;
extern t_external_string const		k_default_victory_condition;

namespace 
{ // unnamed namespace

    t_external_string const     k_text_back( "scene_info_back.misc" );
    t_external_string const     k_text_victory_title( "scene_info_victory_title.misc" );
    t_external_string const     k_text_loss_title( "scene_info_loss_title.misc" );
    t_external_string const     k_text_teams( "scene_info_teams.misc" );
    t_external_string const     k_text_difficulty( "scene_info_difficulty.misc" );
    t_external_string const     k_text_player_difficulty( "scene_info_player_difficulty.misc" );
    t_external_string const     k_text_map_difficulty( "scene_info_map_difficulty.misc" );
    
    t_bitmap_group_cache        k_scenario_info_bitmaps ("dialog.scenario_information");

} // end unnamed namespace

t_scenario_info_window::t_scenario_info_window( t_window* parent, t_adventure_map* map_ptr )
                   : t_window( k_completely_transparent, parent )
{
	t_screen_rect rect;

	m_bitmaps_ptr = k_scenario_info_bitmaps.get();
	m_difficulty_bitmaps_ptr = g_difficulty_60_icons.get();
	
    t_screen_rect parent_rect;
	parent_rect = get_parent()->get_client_rect();
	rect = m_bitmaps_ptr->get_rect();
	rect += (parent_rect.size() - rect.size()) / 2;
	init( rect );
	
    /*
    ** Create background bitmap.
    */
	t_bitmap_layer const*   layer;
	t_window*               window;
	t_window*               background;
	t_screen_point          origin(0,0);
	
    layer = m_bitmaps_ptr->find( "background" );
	background = new t_bitmap_layer_window( layer, origin, this );

    // Text windows.
    t_text_window*          text_window_ptr;

    set_text( "map_name", map_ptr->get_map_name() );
    set_text( "map_description_title", k_text_map_description_title );
    set_text( "map_description", map_ptr->get_map_description(), 18 );
    set_text( "victory_title", k_text_victory_title );
    set_text( "loss_title", k_text_loss_title );
    set_text( "difficulty", k_text_difficulty );
    set_text( "player_difficulty_text", k_text_player_difficulty );
    set_text( "map_difficulty_text", k_text_map_difficulty );
    set_text( "teams", k_text_teams );

	t_player const& player = map_ptr->get_current_player();

    rect = m_bitmaps_ptr->find( "victory_condition" )->get_rect();
	text_window_ptr  = new t_text_window( get_font( 18 ), rect, this, "", t_pixel_24(0,0,0) );
    text_window_ptr->set_text( map_ptr->get_victory_condition_string( player.get_color() ) );
	text_window_ptr->set_center_horizontal();
    
    rect = m_bitmaps_ptr->find( "loss_condition" )->get_rect();
	text_window_ptr  = new t_text_window( get_font( 18 ), rect, this, "", t_pixel_24(0,0,0) );
    text_window_ptr->set_text( map_ptr->get_loss_condition_string( player.get_color() ) );
	text_window_ptr->set_center_horizontal();
    
    // Show difficulty.
	t_help_block const&		help = get_help_block( "scenario_information" );
	t_help_block const&		shared_help = get_help_block( "shared" );
    
    t_difficulty player_difficulty = map_ptr->get_player_difficulty();
	rect = m_bitmaps_ptr->find( "player_difficulty" )->get_rect();
    layer = m_difficulty_bitmaps_ptr->find( k_difficulty_icon_names[ player_difficulty ] );
	window = new t_bitmap_layer_window( layer, rect.top_left(), this );
	set_help ( window, shared_help, k_difficulty_help_names[ player_difficulty ] );
    
    t_difficulty map_difficulty = map_ptr->get_map_difficulty();
	rect = m_bitmaps_ptr->find( "map_difficulty" )->get_rect();
    layer = m_difficulty_bitmaps_ptr->find( k_difficulty_icon_names[ map_difficulty ] );
	window = new t_bitmap_layer_window( layer, rect.top_left(), this );
	set_help ( window, shared_help, k_difficulty_help_names[ map_difficulty ] );

    // Create buttons.
    t_button*   button_ptr;

	rect = m_bitmaps_ptr->find( "back" )->get_rect();
	button_ptr = new t_button( g_large_button.get(), rect.top_left(), background, k_text_back );
	button_ptr->set_click_handler( bound_handler( *this, &t_scenario_info_window::back_click ));
	set_help ( button_ptr, help, "close");	

    show_team_flags( map_ptr );
}

void t_scenario_info_window::set_text( char const* label, std::string text, int height, bool center )
{
	t_screen_rect   rect;
    t_text_window*  text_window_ptr;
    int             font_height = height;

    rect = m_bitmaps_ptr->find( label )->get_rect();
    if (font_height == 0)
        font_height = rect.height();
	text_window_ptr  = new t_text_window( get_font( font_height ), rect, this, text, t_pixel_24(0,0,0) );
    if (center)
	    text_window_ptr->set_center_horizontal();
}

void t_scenario_info_window::show_team_flags( t_adventure_map* map_ptr )
{
	t_player_list const&    players = map_ptr->get_players();
	t_bitmap_group_ptr      flag_bitmaps_ptr = g_banners_55.get();
    t_window *              window_ptr;
    t_window *              flag_window_ptr;
    t_bitmap_layer const*   flag_layer;
    std::string             flag_label;
    std::string             team_label;
    t_screen_rect           flag_rect;
	t_help_block const&		help = get_help_block( "scenario_information" );
    int                     i;
    int                     team_number;

    // Set flag color and team number.
    for (i=0; i<players.size(); i++)
    {
	    t_player_color player_color = players[i]->get_color();

        flag_label = format_string("flag_%d", i+1);
	    flag_rect = m_bitmaps_ptr->find( flag_label )->get_rect();
        window_ptr = new t_window ( flag_rect, k_completely_transparent, this );
        flag_layer = flag_bitmaps_ptr->find( k_player_color_keyword[ player_color ] );
        flag_window_ptr = new t_bitmap_layer_window( flag_layer, t_screen_point( 0, 0 ), window_ptr );

        team_number = players[i]->get_team();
        team_label = format_string("team_%d", team_number+1);
        window_ptr = new t_window ( flag_rect, k_completely_transparent, this );
        flag_layer = flag_bitmaps_ptr->find( team_label );
        flag_window_ptr = new t_bitmap_layer_window( flag_layer, t_screen_point( 0, 0 ), window_ptr );
	    set_help ( flag_window_ptr, help, "team_id");
    }
}

void t_scenario_info_window::back_click( t_button *button_ptr )
{
    close();
}

